package cn.pli.bike.ui;

import android.animation.ValueAnimator;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.LinearInterpolator;

import com.framemodule.base.BaseActivityPresenter;
import com.framemodule.base.netstatus.NetUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import cn.bingoogolapple.androidcommon.adapter.BGAOnRVItemClickListener;
import cn.pli.bike.R;
import cn.pli.bike.bean.NoticeBean;
import cn.pli.bike.bean.UserBean;
import cn.pli.bike.bean.UserInfoSetBean;
import cn.pli.bike.utils.EventBusAction;

import static cn.pli.bike.ui.SettingDelegate.CHECK_UPDATE;
import static cn.pli.bike.ui.SettingDelegate.CLEAR_CACHE;

public class OpenLockActivity extends BaseActivityPresenter<OpenLockDelegate> implements View.OnClickListener {
    ValueAnimator viewAnimator;


    @Override
    protected void initModel() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startCountdown(120);
        EventBus.getDefault().register(this);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected void bindEvenListener() {
        super.bindEvenListener();
        viewDelegate.setOnClickListener(this, R.id.iv_title_back);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.iv_title_back:
                finish();
                break;

        }


    }

    private void startCountdown(int count) {
        viewAnimator = ValueAnimator.ofInt(count, 0);
        viewAnimator.setDuration(count * 1000);
        viewAnimator.setInterpolator(new LinearInterpolator());
        viewAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int i = (int) animation.getAnimatedValue();

                if (i == 2) {
                    if (viewDelegate != null) {
                        viewDelegate.toast("开锁失败");
                    }
                }
                if (i == 0) {
                    finish();
                }

            }
        });
        viewAnimator.start();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handlerAction(Message messageEvent) {

        if (messageEvent.what == EventBusAction.PUSH_ACTION) {
            NoticeBean noticeBean = (NoticeBean) messageEvent.obj;
            switch (noticeBean.getType()) {
                //开锁成功
                case NoticeBean.LOCK_OPEN:
                    viewDelegate.toast("开锁成功");
                    finish();
                    break;
                //关锁成功
                case NoticeBean.LOCK_CLOSE:
                    viewDelegate.toast("关锁成功");
                    break;
            }

        } else if (messageEvent.what != EventBusAction.OPEN_LOCK_BLUE_ERROE) {
            viewDelegate.toast("开锁失败");
            finish();
        }


    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
